/*
 playname:视频来源名称
 PV:视频ID
*/

//判断用户是否登录
function checkcookie(){
    if(document.cookie.indexOf('auth=') >= 0){
        return true;
    }
    return false;
}

var html = '',
    purl = '',
    pvars = '',
    data = [],
    vid = '',
    h = 535,
    pv = playConfig.pv,
    playname = playConfig.playname,
    playJx = playConfig.ckUrl,
    NextWebPage = playConfig.NextWebPage;
    localStorage.ckplayer = 0;
var Player = {
    isMobile: function(){
        var ua = navigator.userAgent;
        var ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
            isIphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
            isAndroid = ua.match(/(Android)\s+([\d.]+)/),
            isMobile = isIphone || isAndroid;
        if(isMobile) {
            return true
        }else{
            return false
        }
    },
    //判断来源
    utm_source: function(){
        var ref = '';
        if (document.referrer.length > 0) {
            ref = document.referrer;
        }try {
            if (ref.length == 0 && opener.location.href.length > 0) {
                ref = opener.location.href;
            }
        } catch (e) {}
        console.log(ref);
        return ref;
    },
    ck: function(type, vid, isfree){
        if(isfree || type === 'tudou'){
            return 'https://api.flvsp.com/?type='+ type +'&vid='+ vid;
        }else if(type === 'flvsp'){
            return 'https://api.flvsp.com/?url=' + vid;
        }else if(type === '47ks'){
            return 'https://api.47ks.com/webcloud/?v=&url=' + vid
        }else if(type === 'qq' || type === 'sohu' || type === 'youku') {
            return type === 'sohu' && this.isMobile() ? playJx + type +'&vid='+ vid : 'https://jx.maoyun.tv/?type='+ (type === 'iqiyi' ?  type = 'iqiyiclient' : type) +'&id=' + vid.split(/\,|_/)[0];
        } else {
            return playJx + type +'&vid='+ vid;
        }
    },
    rePlayUrl: function(pv,playname,isfree){
        var sVid = '',
            sName = '';
        if(pv.indexOf('@@') != -1) {
            data = pv.split('@@');
            playname = data[1];
            pv = data[0];
        }
        switch(playname){
            case 'tudou':
            data = pv.split(',');
            var len = data.length;
            if(len === 1){
                sVid = pv;
                sName = 'youku';
            }else if(len === 2){
                sVid = data[0];
                sName = 'tudou'
            }else if(len === 3){
                sVid = data[2];
                sName = 'youku';
            }
            break;
            case 'youku':
            data = pv.split(',');
            sName = 'youku'
            data.length === 3 ? sVid = data[2] : sVid = pv;
            break;
            case 'letv':
            data = pv.split(',');
            sName = data.length === 1 ? 'letv' : 'letvcloud';
            sVid = data.length === 1 ? pv : data[1] + ':' + data[0];
            break;
            case 'letvyun':
            data = pv.split(',');
            sName = 'letvcloud';
            sVid = data[1] + ':' + data[0];
            break;
            // case 'bilibilick':
            // sName = 'bilibili';
            // sVid = pv;
            // break;
            case 'iqiyi':
            sName = 'iqiyi';
            sVid = pv.indexOf("&tvid=") != -1 ? pv.split('&tvid=')[1] + ',' + pv.split('&tvid=')[0] : pv;
            break;
            case 'sohu':
            data = pv.split('_');
            sVid = data.length === 2 ? data[0] : pv;
            sName = data.length === 2 ? (this.isMobile() ? 'mysohu' : 'sohu') : playname;
            break;
            case 'pptv':
            data = pv.split(',');
            data.length === 2 ? sVid = data[0] : sVid = pv;
            sName = playname;
            break;
            // case 'bilibili':
            // data = pv.split(',');
            // sVid = data.length === 2 ? 'http://www.bilibili.com/video/av' + data[0] + '/index' + data[1] + '.html' : 'http://www.bilibili.com/video/av'+ pv +'/';
            // sName = playname;
            // break;
            case 'acfun':
            data = pv.indexOf('ab') != -1 ? pv.split('ab')[1].split(',') : pv.split(',');
            sVid = data.length === 2 ? 'http://www.acfun.cn/v/' + (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + data[0] + '_' + data[1] : 'http://www.acfun.cn/v/' + (pv.indexOf('ab') != -1 ? ('ab' + data[0] + '_1') : ('ac' + pv));
            sName = playname;
            break;
            default:
            sVid = pv;
            sName = playname;
            break;
        }
        return this.ck(sName,sVid, isfree);
    },
    isJump: function(pv,playname){
        switch(playname){
            case 'youku':
            this.youku(pv);
            break;
            case 'tudou':
            this.tudou(pv);
            break;
            case 'iqiyi':
            this.iqiyi(pv);
            break;
            case 'viqiyi':
            this.iqiyi(pv);
            break;
            case 'letv':
            this.letv(pv);
            break;
            case 'letvyun':
            this.letv(pv);
            break;
            case 'sohu':
            this.sohu(pv);
            break;
            case 'pptv':
            this.pptv(pv);
            break;
            case 'qq':
            this.qq(pv);
            break;
            case 'bilibili':
            this.bilibili(pv);
            break;
            case 'acfun':
            this.acfun(pv);
            break;
        }
    },
    HTML: function(pv){
        html = this.isMobile() ? '<a class="html" target="_blank" href="'+pv+'">点击播放</a>' : '<div class="explaywrap" style="height:'+ h +'px;"><a target="_blank" href="'+pv+'">亲，请点我播放</a><p>该视频需要跳转播放<br>请点击上面的按钮哦</p></div>';
    },
    H5: function(pv){
        html = this.isMobile() ? '<div class="js-media-player"><video webkit-playsinline class="playheight" autoplay controls src="'+pv+'"></video></div>' : '<div class="js-media-player"><video id="mp4" width="100%" height="'+h+'" autoplay controls src="'+pv+'"></video></div>';
    },
    ykUrl: function(data){
        return 'http://player.youku.com/embed/'+ data +'?client_id=08fa721d0f5abf37&autoplay=true';
    },
    jiexiUrl: function(url,h){
        if(Player.isMobile()){
            html = '<iframe class="playheight" style="height: '+ (window.location.href.indexOf('article') != -1 ? '19.333rem' : '3.6rem') +'" src="' + url +'" frameborder="0" scrolling=no allowfullscreen id="ckplayer"></iframe>';
        }else{
            html = '<iframe src="' + url +'" width="100%" height="'+ h +'" frameborder="0" scrolling=no allowfullscreen id="ckplayer"></iframe>';
        }
        return html;
    },
    jxUrl: function(pv, playname, isfree, NextWebPage, Sid){
        var params = (playname === 'qq' || playname === 'youku' || playname === 'sohu' && !this.isMobile()) ? '' : ((Player.rePlayUrl(pv,playname,isfree).indexOf('flvsp') != -1 ? '&next=' : '&nextPage=') + (parseInt(Sid) === 1 ? (NextWebPage !== '0' ? window.location.origin + NextWebPage : '') : '') + '&hd=4&userlink=' + window.location.href);
        return jiexiUrl = Player.rePlayUrl(pv,playname,isfree) + params;
    },
    tdUrl: function(data){
        if(this.isMobile()){
            purl = 'http://www.tudou.com/programs/view/html5embed.action?type=0&code='+ data +'&lcode=&resourceId=0_06_05_99';
        }else{
            html = '<embed width="100%" height="100%" id="youku" src="http://www.tudou.com/v/'+ data +'/&withRecommendList=false&videoClickNavigate=false&withSearchBar=false&withRecommendList=false/v.swf&totalTime=1&autoPlay=true" wmode="transparent" flashvars="" allowfullscreen="true" type="application/x-shockwave-flash">';
        }
    },
    iframe: function(pv){
        html = this.isMobile() ? '<iframe class="playheight" src="'+ pv +'" frameborder="0" scrolling=no allowfullscreen></iframe>' : '<iframe src="' + pv + '" width="100%" scrolling="no" height="'+ h +'" frameborder="0" scrolling=no allowfullscreen id="ckplayer"></iframe>';
    },
    flash: function(url,flashvars){
        html = '<object id="flashBox" height="' + h + '" style="visibility:visible;" width="100%" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"><param value="' + url + '"><param value="high" name="quality"><param value="never" name="allowScriptAccess"><param value="true" name="allowFullScreen"><param value="'+ flashvars +'" name="flashvars"><param value="transparent" name="wmode"><embed id="flashBox" height="' + h + '" allowscriptaccess="never" style="visibility:visible;" pluginspage="http://get.adobe.com/cn/flashplayer/" flashvars="'+ flashvars +'" width="100%" allowfullscreen="true" quality="high" src="' + url + '" type="application/x-shockwave-flash" wmode="transparent"></object>';
    },
    tudou: function (pv) {
        data = pv.split(',');
        var len = data.length;
        if (len === 1) {
            this.youku(pv)
        } else if(len === 2) {
            this.tdUrl(data[1])
        }else if(len >= 3){
            this.youku(data[2])
        }
    },
    youku: function (pv) {
        data = pv.split(',');
        this.iframe(this.ykUrl(data.length === 3 ? data[2] : pv));
    },
    iqiyi: function (pv) {
        var plus = this.isMobile() ? '&tvid=' : '&tvId=';
        var pid = '';
        var _this = this;
        if(pv.indexOf(",")>0){
            data = pv.split(',');
            vid = data[1] + plus + data[0];
            pid = data[0];
        }else if(pv.indexOf("&tvid=")>0){
            data = pv.split('&tvid=');
            vid = data[0] + plus + data[1];
            pid = data[1];
        }else if(pv.indexOf("_")>0){
            data = pv.split('_');
            vid = data[1] + plus + data[0];
            pid = data[0];
        }
        if(noLogin === 'vip'){
            var IQIYI = '//www.ikanfan.cn/api/iqiyi-id.php?pid='+ pid;
            $.getJSON(IQIYI, function(data){
                _this.HTML(data.data.videoUrl);
                $('.playerbox').html(html)
            });
        }else{
            if(this.isMobile()){
                purl = 'http://m.iqiyi.com/shareplay.html?vid='+ vid +'&coop=coop_117_9949&cid=qc_105102_300452&bd=1&autoplay=1&fullscreen=1';
                this.iframe(purl);
            }else{
                purl = 'http://www.iqiyi.com/common/flashplayer/20170406/1556f98c2359.swf?menu=false&autoplay=true&cid=qc_100001_100100&flashP2PCoreUrl=http://www.iqiyi.com/common/flashplayer/20170406/15562a1b82aa.swf&=undefined&&definitionID='+ vid +'&isPurchase=0&cnId=4&coop=ugc_openapi_wanyouwang&cid=qc_100001_300089&bd=1&autoChainPlay=1&showRecommend=0&source=&purl=&autoplay=true';
                pvars = '';
                this.flash(purl,pvars);
            }
        }
    },
    letv: function (pv) {
        data = pv.split(',');
        var len = data.length;
        if(this.isMobile()){
            if (len == 1) {
                purl = 'http://minisite.letv.com/tuiguang/index.shtml?vid='+ pv +'&ark=76&continuration=0&isPlayerAd=0&auto_play=1&autoplay=1&light=0&extend=0';
            }else if(len==2) {
                purl = 'http://yuntv.letv.com/bcloud.html?uu='+ data[0] +'&vu='+ data[1] +'&auto_play=1&gpcflag=1&width=100%&height=100%';
            }
            this.iframe(purl);
        }else{
            if (len == 1) {
                purl = 'http://player.hz.letv.com/hzplayer.swf/open';
                pvars = 'ark=76&autoplay=1&loadingUrl=undefined&vid=' + pv + '&continuration=0&preload=1&autoMute=0&forceCallback=1&barrage=0&camera=0&isHttps=0&p1=1&p2=10&forceCDN=1&lan=cn&region=cn&callbackJs=vjs_callback_148427711492037&hostnamestr=letv'
            } else if (len == 2) {
                purl = 'http://yuntv.letv.com/bcloud.swf?uu=' + data[0] + '&vu=' + data[1] + '&auto_play=1&gpcflag=1?' + pvars;
                pvars = 'MMControl=false&MMout=false';
            }
            this.flash(purl,pvars);
        }
    },
    sohu: function (pv) {
        if(pv.indexOf("_")>0){
            data = pv.split('_');
            purl = this.isMobile() ? 'http://tv.sohu.com/upload/static/play/iplay.html?&bid='+ data[0] +'&autoplay=true&h5=true&src=11510001&lqd=24352&dlBanner=true&hotVideo=true' : 'http://share.vrs.sohu.com/my/v.swf&topBar=0&id=' + data[0] + '&skinNum=1&showRecommend=0&autoplay=true&api_key=b24ab6248dace426097bb7b35df84c7c&sogouBtn=0';
            this.isMobile() ? this.iframe(purl) : this.flash(purl,pvars);
        }else{
            var b=new Date().getTime();
            var a=Math.floor(b*3.268);
            var c=Math.floor(pv*2.5);
            purl = this.isMobile() ? 'http://hot.vrs.sohu.com/ipad'+ pv +'_'+a+'_'+c+'.m3u8?plat=null&prod=app' : 'http://share.vrs.sohu.com/' + pv + '/v.swf&skinNum=1&topBar=0&showRecommend=0&autoplay=true&api_key=b24ab6248dace426097bb7b35df84c7c&sogouBtn=0';
            this.isMobile() ? this.H5(purl) : this.flash(purl,pvars);
        }
    },
    pptv: function (pv) {
        data = pv.split(',');
        if(this.isMobile()){
            purl = 'http://m.pptv.com/show/'+ data[0] + '.html?rcc_src=P5';
            this.HTML(purl);
        }else{
            purl = 'http://player.pptv.com/iframe/index.html?ctx=o=v_share#id=' + data[0];
            html = '<iframe id="flashBox" src="' + purl + '" allowtransparency="true" width="100%" height="' + h + '" scrolling="no" frameborder="0" ></iframe>';
        }
    },
    qq: function (pv) {
        if(this.isMobile()){
            purl = 'http://v.qq.com/iframe/player.html?vid='+ pv +'&tiny=0&auto=1'
            this.iframe(purl);
        }else{
            purl = 'https://imgcache.qq.com/tencentvideo_v1/playerv3/TPout.swf';
            pvars = 'vid='+ pv +'&tpid=0&showend=1&showcfg=1&searchbar=1&shownext=1&list=2&autoplay=0&bullet=0&showlogo=0&searchpanel=0&showend=0&showcfg=0&autoplay=1&follow=0&clientbar=0';
            this.flash(purl,pvars);
        }
    },
    bilibili: function (pv) {
        data = pv.split(',');
        purl = data.length === 2 ? 'http://www.bilibili.com/video/av' + data[0] + '/index' + data[1] + '.html' : 'http://www.bilibili.com/video/av'+ pv +'/';
        this.HTML(purl);
    },
    acfun: function (pv) {
        if(pv.indexOf('ab') != -1){
            data = pv.split('ab');
            var ab = data[1].split(',')
            if(ab.length === 2){
                vid = ab[0] +'_'+ ab[1];
            }else{
                vid = data[1];
            }
        }else{
            data = pv.split(',');
            var len = data.length;
            if(len == 2){
                vid = data[0] +'_'+ data[1];
            }else{
                vid = pv;
            }
        }
        if(this.isMobile()){
            purl = 'http://m.acfun.cn/v/?'+ (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + '='+ vid;
            this.HTML(purl);
        }else{
            purl = 'http://www.acfun.cn/v/'+ (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + vid;
            this.HTML(purl);
        }
    }
}


function Play(pv,playname){
    var isfree = false;
    html = '';
    if(pv.indexOf('@@')>0) {
        data = pv.split('@@');
        playname = data[1];
        pv = data[0];
        isfree = data[2] === 'free' ? true : false;
    }
    if(pv.indexOf(".html") !== -1 || pv.indexOf(".shtml") !== -1 || pv.indexOf(".htm") !== -1 || pv.indexOf(".mp4") !== -1 || playname === 'full'){
        if(playname === '47ks'){
            localStorage.ckplayer = 1;
            html = Player.jiexiUrl(Player.jxUrl(pv, playname, isfree, NextWebPage, Sid),495);
        }else if(playname === 'flvsp'){
            localStorage.ckplayer = 1;
            html = Player.jiexiUrl(Player.jxUrl(pv, playname, isfree, NextWebPage, Sid),495);
        }else if(playname === 'full'){
            localStorage.ckplayer = 1;
            html = Player.jiexiUrl(pv,495);
        }else{
            localStorage.ckplayer = 0;
            if(window.location.hostname.indexOf('xichen.tv') !== -1 && pv.indexOf("youku.com") === -1){
                localStorage.ckplayer = 1;
                var n = 2;
                n = pv.indexOf("v.qq.com") !== -1 ? 3 : n;
                html = Player.jiexiUrl('http://www.ikanfan.cn/api/?url=' + pv + '&n=' + n, 495);
            }else{
                Player.HTML(pv);
            }
        }
    }else if( pv.indexOf(".mp4") != -1 ){
        localStorage.ckplayer = 0;
        Player.H5(pv);
    }else{
        playStyle = ((Player.isMobile() ? true : checkcookie() ? true : false) || isfree || data[2] === 'ck' || window.location.search.indexOf('99496.com') != -1 || window.location.hostname.indexOf('xichen.tv') != -1 || noLogin === 'play' || playname === 'acku' || playname === 'sina' || playname === 'letvsaas' || playname === 'mmsid' || playname === 'huya' || playname === 'mgtv' || playname === 'qqmtv' || playname === 'ppbox' || playname === 'kankan' || playname === 'toutiao' || playname === 'weibo' || playname === 'miaopai' || playname === '56' || playname === '1905' || playname === '17173' || playname === 'meipai' || playname === 'kuaishou' || playname === 'file' || playname === 'bilibilick' || playname === 'kuvod' || Player.utm_source().indexOf('neets') !== -1 || (pv.split(',').length === 2 && playname === 'tudou')) ? true : false;
        if(playname === 'bilibili' || parseInt(data[2]) === 1 || noLogin === 'vip' || playname === 'acfun'){
            Player.isJump(pv,playname);
        }else{
            if(playStyle && !(parseInt(data[2]) === 1) && noLogin != 'vip'){
                localStorage.ckplayer = 1;
                html = Player.jiexiUrl(Player.jxUrl(pv, playname, isfree, NextWebPage, Sid),495);
            }else{
                Player.isJump(pv, playname);
            }
        }
    }
    html && $('.playerbox').html(html)
    if(!Player.isMobile()){
        $('.play-box').height(parseInt(localStorage.ckplayer) ? h - 40 : h);
        listHeight()
    }
}

(playname && playname != '' && pv && pv != '') ? Play(pv,playname) : window.location.href = window.location.origin;

function listHeight(){
    if(pv.indexOf(".mp4") > 0){
        $('.tab-main').height(h-142);
    }else if(parseInt(localStorage.ckplayer)){
        $('.tab-main').height(h-150);
    }else{
        $('.tab-main').height(h-110);
    }
}

function closefull(){
    $('.play-box').removeClass('full');
    $('.webexit').remove();
    $('.gbtn').show();
    $('#cs_right_bottom, #ft_couplet_right, #ft_couplet_left').show();
    $('body,#tudou,#tudou2,#iqiyi,#flashBox,#mp4,#ckplayer').attr('style','');
    $('#tudoudiv').css({'overflow':'hidden','height':h + 'px'});
    $('.play-box').css('height',parseInt(localStorage.ckplayer) ? h - 40 : h);
    listHeight()
}
function openfull(){
    $('.play-box').addClass('full');
    $('.gbtn').hide();
    $('#cs_right_bottom, #ft_couplet_right, #ft_couplet_left').hide();
    $('body').css('overflow','hidden').append('<span class="webexit">退出全屏</span>');
    $('#flashBox,#iqiyi,#mp4,#ckplayer').css({position: 'absolute',height: '100%','z-index': 99});
    $('#tudoudiv,.play-box').attr('style','');
    listHeight()
}

if(!Player.isMobile()){
    function closefull(){
        $('.play-box').removeClass('full');
        $('.webexit').remove();
        $('.gbtn').show();
        $('#cs_right_bottom, #ft_couplet_right, #ft_couplet_left').show();
        $('body,#tudou,#tudou2,#iqiyi,#flashBox,#mp4,#ckplayer').attr('style','');
        $('#tudoudiv').css({'overflow':'hidden','height':h + 'px'});
        $('.play-box').css('height',parseInt(localStorage.ckplayer) ? h - 40 : h);
        listHeight()
    }
    function openfull(){
        $('.play-box').addClass('full');
        $('.gbtn').hide();
        $('#cs_right_bottom, #ft_couplet_right, #ft_couplet_left').hide();
        $('body').css('overflow','hidden').append('<span class="webexit">退出全屏</span>');
        $('#flashBox,#iqiyi,#mp4,#ckplayer').css({position: 'absolute',height: '100%','z-index': 99});
        $('#tudoudiv,.play-box').attr('style','');
        listHeight()
    }
    function listHeight(){
        if(pv.indexOf(".mp4") > 0){
            $('.tab-main').height(h-142);
        }else if(parseInt(localStorage.ckplayer)){
            $('.tab-main').height(h-150);
        }else{
            $('.tab-main').height(h-110);
        }
    }
    $(document).on('click','.webexit,.websize,.webfullbtn',function(){
        if($('.play-box').hasClass('full')){
            closefull();
        }else{
            openfull();
        }
    });

    $(document).on('dblclick','.playbg',function(){
        if($('.play-box').hasClass('full')){
            closefull();
        }else{
            openfull();
        }
    });

    $(document).keydown(function(e){
        var code = e.keyCode ? e.keyCode : e.which;
        if(code == 27 || code == 96){
            closefull();
        }
    });

    $(function(){
        listHeight()
        var timer = null;
        $('.play-box').hover(function(e) {
            clearTimeout(timer);
            $('.webexit').show();
        }, function(e) {
            timer = setTimeout(function(){
                $('.webexit').hide();
            },3000);
        });
    });
}
